package com.xiangxiao.rpan.settings.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xiangxiao.rpan.settings.Application;
import com.xiangxiao.rpan.settings.constant.GlobalConsts;
import com.xiangxiao.rpan.settings.constant.ResponseCode;
import com.xiangxiao.rpan.settings.constant.ResponseMessage;
import com.xiangxiao.rpan.settings.consumer.IConsumerStorageClient;
import com.xiangxiao.rpan.settings.consumer.callback.StorageClientCall;
import com.xiangxiao.rpan.settings.dto.FileListConfigDto;
import com.xiangxiao.rpan.settings.dto.JsonObject;
import com.xiangxiao.rpan.settings.dto.SystemConfigDTO;
import com.xiangxiao.rpan.settings.exception.SettingsException;
import com.xiangxiao.rpan.settings.service.SystemConfigService;
import com.xiangxiao.rpan.settings.vo.SiteConfigVo;
import com.xiangxiao.rpan.settings.vo.StorageSourceConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/6/9 15:14
 */
@Slf4j
@RequestMapping("/api/site")
@RestController
public class SiteController {
  private static Logger logger = LoggerFactory.getLogger(SiteController.class);
  @Autowired
  private SystemConfigService systemConfigService;
  @Resource
  private IConsumerStorageClient consumerStorageClient;

  @GetMapping("/config/global")
  public JsonObject globalConfig() {
    try {
      SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();

      SiteConfigVo siteConfigResult = new SiteConfigVo();
      BeanUtils.copyProperties(systemConfig, siteConfigResult);

//    siteConfigResult.setDebugMode(zFileProperties.isDebug());
      return new JsonObject(ResponseMessage.QUERY_CONFIG_SUECCESS, ResponseCode.OK, siteConfigResult);
    } catch (SettingsException e) {
      logger.error(ResponseMessage.QUERY_CONFIG_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_CONFIG_FAILED, ResponseCode.QUERY_CONFIG_FAILED);
    }
  }

  @GetMapping("/domain")
  public JsonObject getDomain() {
    try {
      String domain = systemConfigService.getDomain();

      return new JsonObject(ResponseMessage.QUERY_CONFIG_DOMAIN_SUECCESS, ResponseCode.OK, domain);
    } catch (SettingsException e) {
      logger.error("获取站点domain设置失败", e.getMessage());
      return new JsonObject(ResponseMessage.QUERY_CONFIG_DOMAIN_FAILED, ResponseCode.QUERY_CONFIG_DOMAIN_FAILED);
    }
  }

  @PostMapping("/config/storage")
  public JsonObject storageList(@Valid @RequestBody FileListConfigDto fileListConfigDto) {
    try {
      JSONObject resp = consumerStorageClient.queryStorageConfigs(fileListConfigDto);
      StorageSourceConfigVo configVo = resp.getObject("data", new TypeReference<StorageSourceConfigVo>() {});
      return new JsonObject(ResponseMessage.QUERY_STORAGE_CONFIG_SUECCESS, ResponseCode.OK, configVo);
    } catch (SettingsException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_STORAGE_CONFIG_FAILED, ResponseCode.QUERY_STORAGE_CONFIG_FAILED);
    }
  }

}
